package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.progressindicator.CircularProgressIndicator;
import com.google.android.material.progressindicator.LinearProgressIndicator;

import cn.lrwjz.androidguide.R;

public class ProgressIndicatorActivity extends AppCompatActivity {
    private LinearProgressIndicator determinateLinear;
    private LinearProgressIndicator indeterminateLinear;
    private CircularProgressIndicator determinateCircular;
    private CircularProgressIndicator indeterminateCircular;
    private int currentProgress = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress_indicator);

        // 设置工具栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("Progress Indicator 示例");
        }

        // 初始化进度指示器
        initProgressIndicators();
        setupButtons();
        startProgressAnimation();
    }

    private void initProgressIndicators() {
        determinateLinear = findViewById(R.id.determinateLinear);
        indeterminateLinear = findViewById(R.id.indeterminateLinear);
        determinateCircular = findViewById(R.id.determinateCircular);
        indeterminateCircular = findViewById(R.id.indeterminateCircular);

        // 设置初始进度
        determinateLinear.setProgress(0);
        determinateCircular.setProgress(0);
    }

    private void setupButtons() {
        MaterialButton btnShow = findViewById(R.id.btnShow);
        MaterialButton btnHide = findViewById(R.id.btnHide);

        btnShow.setOnClickListener(v -> {
            determinateLinear.show();
            indeterminateLinear.show();
            determinateCircular.show();
            indeterminateCircular.show();
        });

        btnHide.setOnClickListener(v -> {
            determinateLinear.hide();
            indeterminateLinear.hide();
            determinateCircular.hide();
            indeterminateCircular.hide();
        });
    }

    private void startProgressAnimation() {
        // 创建一个线程来模拟进度更新
        new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(100);
                    currentProgress = (currentProgress + 1) % 100;
                    runOnUiThread(() -> {
                        determinateLinear.setProgress(currentProgress);
                        determinateCircular.setProgress(currentProgress);
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
        }).start();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 